//------------------------------------------------------------------------------
//!	@file	ProcEOCtrl.c
//!	@brief
//!		Process EO Ctrl type LUTs
//!
//!	@Description
//!		This file contains all of the EO based processing.
//!
//!
//!	Originally Developed: July, 1997 to October, 1999
//!
//!	$Id: ProcEOCtrl.c 158 2013-03-03 07:00:15Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//	Copyright and Disclaimer Notice
//
//	Copyright (c) 2013 A.C. Verbeck
//
//	This file is part of CreateLUT: A LUT creation library.
//
//	CreateLUT is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	CreateLUT is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with CreateLUT.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#include <stdint.h>
#include <stdbool.h>

#include "ProcEOCtrl.h"
#include "dsp08.h"
#include "SysParam.h"

//
//	File local variables
//
static FILE	*OutFile;

double	EO_Curve[CURVE_CT][CURVE_SZ_08BIT];

//
//	Local functions
//
static void EO_Load(char* InFile);
static void EO_ProcessLut(void);
static void EO_WriteLut(uint8_t* tab);


//
//	Name:
//		EOCtrl_Execute
//
//	Description:
//
//	Parameters:
//		(None)
//
//	Returns:
//		(None)
//
void EOCtrl_Execute(void)
{
	extern char InFileName[];
	extern char OutFileName[];

//
//	Load the raw EO curves
//
	EO_Load(InFileName);

//
//	Open the output file
//
	printf("Loading EO data\n");
	OutFile = fopen(OutFileName,"wb");
	if (OutFile == NULL) {
		printf("File open error: %s\n",OutFileName);
		exit(1);
	}

//
//	Process the EO curves
//
	printf("Processing EO data:");
	EO_ProcessLut();
	printf("Processing complete.\n");

	fclose(OutFile);
}


//
//	Name:
//		EO_Load
//
//	Description:
//
//	Parameters:
//		char*	InFile:		Input file
//
//	Returns:
//		(No direct return)
//		Indirect: The EO_Curve array is loaded
//
static void EO_Load(char* InFile)
{
	FILE*	f;
	int32_t	i,j,fct;
	int32_t	CurrIdx;

	float	CurrD;

//
//	Open the input file
//
	f = fopen(InFile,"r");
	if (f == NULL) {
		printf("File open error: %s\n",InFile);
		exit(1);
	}
	CurrD = 0.0;

//
//	Read the curves
//
	for (j=0; j<CURVE_SZ_08BIT; j++) {

		//	Read the index
		if ((fct = fscanf(f,"%d",&CurrIdx)) == EOF) {
			printf("EOF encountered!\n");
			exit(1);
		}
		if (CurrIdx != j) {
			printf("Index error\n");
			exit(1);
		}

		//	Read the data
		for (i=0; i<CURVE_CT; i++) {
			if ((fct = fscanf(f,"%f",&CurrD)) == EOF) {
				printf("EOF encountered!\n");
				exit(1);
			}
			EO_Curve[i][j] = CurrD;
		}
	}
}


//
//	Name:
//		EO_ProcessLut
//
//	Description:
//
//	Parameters:
//		(No direct parameters)
//		Indirect: The EO_Curve array should be loaded
//
//	Returns:
//		(No direct return)
//		Indirect: The EO_Curve array is loaded
//
static void EO_ProcessLut()
{
	double	GainIdx,GainInc;
	double	FilterCurve[CURVE_SZ_08BIT];
	uint8_t	OutTab[CURVE_SZ_08BIT];
	int32_t	i,j;

	extern double	StartGain;
	extern double	EndGain;
	extern int32_t	LutGainCt;

	for (i=0; i<CURVE_CT; i++) {
		printf(".");
		DSP08_Filter00(FilterCurve, EO_Curve[i]);
		GainInc  = (EndGain-StartGain)/(double)(LutGainCt-1);

		for (GainIdx=StartGain,j=0; j<LutGainCt; GainIdx+=GainInc,j++) {
			memset(OutTab, 0, CURVE_SZ_08BIT);
			DSP08_BuildLUT(OutTab, FilterCurve, GainIdx, GAMMA_VAL);
			EO_WriteLut(OutTab);
		}
	}
	printf("\n");
}


//
//	Name:
//		EO_WriteLut
//
//	Description:
//
//	Parameters:
//		(No direct parameters)
//		Indirect: The EO_Curve array should be loaded
//
//	Returns:
//		(No direct return)
//		Indirect: The EO_Curve array is loaded
//
static void EO_WriteLut(uint8_t* tab)
{
	fwrite(tab, 1, CURVE_SZ_08BIT, OutFile);
}

//
//	End: ProcEOCtrl.c
//
